package com.scsystem.xysc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scsystem.xysc.aop.LogOperations;
import com.scsystem.xysc.mapper.OrderMapper;
import com.scsystem.xysc.pojo.Order;
import com.scsystem.xysc.pojo.vo.ReturnResult;
import com.scsystem.xysc.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@CrossOrigin
@Tag(name = "订单接口")
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;


    /**
     * 条件查询
     *
     * @param orderId    订单的id
     * @param userId     用户的id
     * @param productId  商品的id
     * @param status     订单状态（待付款/待发货/待收货/待评价/已完成）
     * @param createDate 订单创建时间
     * @param pageNumber 页码
     * @param count      每页显示的数量
     * @return ReturnResult<Page < Order>>封装后的分页对象
     */
    @GetMapping("/select")
    @Operation(summary = "条件查询订单信息，不传条件就是全查")
    @Parameters({
            @Parameter(name = "orderId", description = "订单的id"),
            @Parameter(name = "userId", description = "用户的id"),
            @Parameter(name = "productId", description = "商品的id"),
            @Parameter(name = "status", description = "订单状态（待付款/待发货/待收货/待评价/已完成）"),
            @Parameter(name = "createDate", description = "订单创建时间"),
            @Parameter(name = "pageNumber", description = "页码", required = true),
            @Parameter(name = "count", description = "每页显示的数量", required = true),

    })
    public ReturnResult<Page<Order>> selectOrderPage(
            @RequestParam(required = false) Long orderId,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) Long productId,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) LocalDateTime createDate,
            int pageNumber,
            int count) {
        Order order = new Order();
        if (orderId != null) {
            order.setOrderId(orderId);
        }
        if (userId != null) {
            order.setUserId(userId);
        }
        if (productId != null) {
            order.setProductId(productId);
        }
        if (status != null) {
            order.setStatus(status);
        }
        if (createDate != null) {
            order.setCreateDate(createDate);
        }
        ReturnResult<Page<Order>> returnResult = new ReturnResult<>();
        Page<Order> orderPage = orderService.getOrderPage(order, pageNumber, count);
        if (!orderPage.getRecords().isEmpty()) {
            returnResult.setCode(200);
            returnResult.setMessage("查询成功");
            returnResult.setData(orderPage);
        } else {
            returnResult.setCode(400);
            returnResult.setMessage("查询到的结果为空");
        }
        return returnResult;
    }

    /**
     * 修改订单
     *
     * @param order 修改后的订单对象
     * @return 封装修改后的订单对象
     */
    @PostMapping("/update")
    @Operation(summary = "通过订单id修改订单")
    @Parameter(name = "order", description = "订单对象")
    @LogOperations(module = "订单", operation = "通过订单id修改订单数据")
    public ReturnResult<Order> updateOrder(@RequestBody Order order) {
        if (orderService.updateById(order)) {
            return new ReturnResult<>(200, "修改成功", orderService.getById(order.getOrderId()));
        }
        return new ReturnResult<>(400, "修改失败", orderService.getById(order.getOrderId()));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "通过订单id删除订单")
    @Parameter(name = "orderId", description = "订单id")
    @LogOperations(module = "订单", operation = "通过订单id删除订单数据")
    public ReturnResult<Order> deleteOrder(@RequestParam("orderId") Long orderId) {
        if (orderService.removeById(orderId)) {
            return new ReturnResult<>(200, "删除成功");
        }
        return new ReturnResult<>(400, "删除失败");
    }

    @PostMapping("/add")
    @Operation(summary = "添加订单，传入一个订单对象")
    @Parameter(name = "order", description = "订单对象")
    @LogOperations(module = "订单", operation = "添加订单")
    public ReturnResult<Order> addOrder(@RequestBody Order order) {
        order.setCreateDate(LocalDateTime.now());
        order.setOrderId(null);
        if (orderService.save(order)) {
            return new ReturnResult<>(200, "添加成功", order);
        }
        return new ReturnResult<>(400, "添加失败");
    }

    @PostMapping("/updateOrderStatus")
    @Parameters({
            @Parameter(name = "oid", description = "订单id"),
            @Parameter(name = "status", description = "订单状态（待付款/待发货/待收货/待评价/已完成/售后）")}
    )
    @LogOperations(module = "订单", operation = "修改订单状态")
    public ReturnResult<Boolean> updateOrderStatus(Long oid, String status) {
        Order order = orderService.getById(oid);
        if (order != null){
            if ("待发货".equals(status)) {
                order.setPaymentTime(LocalDateTime.now());
            } else if ("待收货".equals(status)) {
                order.setDeliveryTime(LocalDateTime.now());
            } else if ("待评价".equals(status)) {
                order.setEndTime(LocalDateTime.now());
            }
            order.setStatus(status);
            orderService.updateById(order);
            return new ReturnResult<>(200, "成功", true);
        }
        return new ReturnResult<>(400, "订单信息未找到", false);
    }
}
